import { createRouter, createWebHashHistory } from 'vue-router'
import { Toast } from 'vant'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: () => import('../views/home/Home')
    },
    {
      path: '/home',
      component: () => import('../views/home/Home')
    },
    {
      path: '/order',
      component: () => import('../views/order/Order'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/cart',
      component: () => import('../views/cart/Cart'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/mine',
      component: () => import('../views/mine/Mine'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/store',
      component: () => import('../views/store/Store')
    },
    {
      path: '/createorder',
      component: () => import('../views/createOrder/createOrder'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/address',
      component: () => import('../views/address/Address'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/addressedit',
      component: () => import('../views/addressEdit/AddressEdit'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/userinfoedit',
      component: () => import('../views/userinfoedit/UserInfoEdit'),
      meta:{
        isAuth :true
      }
    },
    {
      path: '/login',
      component: () => import('../views/login/Login')
    },
    {
      path: '/register',
      component: () => import('../views/register/Register')
    }
  ]
})
router.beforeEach((to,from,next) =>{
  if(to.meta.isAuth){
    // 登陆以后 会在localstorage里面 存储一个标识
    if(localStorage.isLogin === 'login'){
      next()
    }else{
      next('/login')
      Toast('请先登录')
    }
  }else{
    next()
  }
})
export default router
